<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use Mail;
use Session;
use Crypt;
class ResetController extends Controller
{
    //reset
	public function reset()
	{
		$conf = DB::table('bro_config') -> first();
		 $link = DB::table('bro_link') -> orderBy('order', 'asc') -> get();
		$title = '找回密码';
		return view('home.reset.resetPass',['title' => $title,'link' => $link,'conf' => $conf]);
	}

	//邮箱验证
    public function email()
    {
    	$email = $_POST['email'];
    	$data = DB::table('bro_user') -> where('email',$email) -> first();
    	// dd($data);
    	if($data)
    	{
    		echo 1;//邮箱存在
            exit;
    	}else{
    		echo 2;//邮箱不存在
            exit;
    	}


    }

	//send
	public function send(Request $request)
	{
		// dd($request -> all());
		$data = DB::table('bro_user') -> where('email',$request -> input('email')) -> first();
		// dd($data);
		
		$flag = Mail::send('mails.send',['data' => $data,'request' => $request],function ($message) use ($request){
			$message -> subject('忘记密码提示');
			$message -> to($request -> input('email'));
		});


		if(!$flag)
		{
			return redirect('/info') -> with(['info' => '发送不成功']);
		}else
		{
			return redirect('/info') -> with(['info' => '重置密码的邮件已经发送到您的邮箱，请注意查收']);
		}
	}
	//info
	public function info()
	{
		$conf = DB::table('bro_config') -> first();
		$link = DB::table('bro_link') -> orderBy('order', 'asc') -> get();
		$title = '找回密码';
		return view('home.reset.info',['title' => $title,'link' => $link,'conf' => $conf]);
	}
	
	//newform	
	public function newform($id, $token)
	{
		$conf = DB::table('bro_config') -> first();
		$link = DB::table('bro_link') -> orderBy('order', 'asc') -> get();
		$title = '密码重置';
		$data = DB::table('bro_user') -> where('id', $id) -> first();
		
		if($data)
		{
			return view('home.reset.newform',['data' => $data,'title' => $title,'link' => $link,'conf' => $conf]);
		}else{
			return redirect('/info') -> with(['info' => '不是一个合法的来源']);
		}
		
	}
	
	//ajax 判断密码是否一致
	public function password()
	{
		// return 111;
		$password = $_POST['password'];
		// return $password;
        $repassword = $_POST['repassword'];

   
        if($password == $repassword)
        {
            echo 1;
        }else{
            echo 2;
        }


	}
	//update
	public function update(Request $request)
	{
		$data = $request -> only('password');
		
		if($request -> input('password') != $request -> input('repassword') || $request -> input('password') == "" || $request -> input('password') == ""  )
		{
			return back();
			// return '确认密码不一样';
		}

		$data['password'] = Crypt::encrypt($data['password']);
		$res = DB::table('bro_user') -> where('id', $request -> input('id')) -> update($data);
		return redirect('/resetok');
	}

	//ok
	public function ok()
	{
		$conf = DB::table('bro_config') -> first();
		$link = DB::table('bro_link') -> orderBy('order', 'asc') -> get();
		$title = '重置密码成功';
		return view('home.reset.resetok',['title' => $title,'link' => $link,'conf' => $conf]);
	}

}
